package br.com.pokertime.modelo;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import br.com.pokertime.database.DBAdapter;

public class BlindDAO extends DBAdapter {

	public BlindDAO(Context ctx) {
		super(ctx);
	}

	public long insert(Blind blind) {
		this.open();
		ContentValues initialValues = fillBlind(blind);
		long retorno = db.insert(Blind.tabela, null, initialValues);
		this.close();
		return retorno;

	}

	public boolean deleteNivel(int nivel) {
		String where = nivel + "=" + new Integer(nivel).toString();
		return db.delete(Blind.tabela, where, null) > 0;
	}

	public List<Blind> getAllBlinds() {
		this.open();
		List<Blind> lista = new ArrayList<Blind>();
		Cursor c = db.query(Blind.tabela, Blind.allColumns, null, null, null,null, null);
		if (c.moveToFirst()) {
			do {
				Blind b = new Blind();
				b.setBigBlind(c.getInt(c.getColumnIndex(Blind.colunaBigBlind)));
				b.setSmallBlind(c.getInt(c.getColumnIndex(Blind.colunaSmallBlind)));
				b.setNivel(c.getInt(c.getColumnIndex(Blind.colunaNivel)));
				lista.add(b);

			} while (c.moveToNext());
		}
		this.close();
		return lista;
	}

	public Cursor getNivel(int nivel) throws SQLException {

		String where = Blind.colunaNivel + "= ?";
		String[] args = new String[] { new Integer(nivel).toString() };
		Cursor mCursor = db.query(true, Blind.tabela, Blind.allColumns, where,args, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean atualizar(Blind blind) {
		ContentValues args = fillBlind(blind);
		String where = Blind.colunaNivel + "=" + blind.getNivel();
		return db.update(Blind.tabela, args, where, null) > 0;
	}
	
	private ContentValues fillBlind(Blind blind){
		ContentValues args = new ContentValues();
		args.put(Blind.colunaBigBlind, blind.getBigBlind());
		args.put(Blind.colunaSmallBlind, blind.getSmallBlind());
		args.put(Blind.colunaNivel, blind.getNivel());
		return args;
	}

}
